#include <QPixmap>
#include <QMessageBox>
#include<QDebug>
#include "WZQ.h"
#include <QGridLayout>
#include <QPainter>

WZQ::WZQ(QWidget *parent) : QWidget(parent)
{
    setFixedSize(540, 540);
   setWindowTitle ("《原神》新卡池七圣召唤挺火，以后也许会有五子棋");
 //  setWindowTitle("今天就让我们与蒙德桥边养鸽子的提米来一场");
    QGridLayout* layout = new QGridLayout(this);
    layout->setSpacing(0);
    layout->setMargin(0);

    for (int i = 0; i < ROW_COUNT; i++) {
        for (int j = 0; j < COL_COUNT; j++) {
            QLabel* cell = new QLabel(this);
            cell->setAlignment(Qt::AlignCenter);
            cell->setFixedSize(36, 36);
            cell->setStyleSheet("QLabel { background-color: rgba(255, 255, 255, 0); border: 1px solid rgb(0, 0, 0); }");
            layout->addWidget(cell, i, j);
        }
    }

    board_.resize(ROW_COUNT);
    for (int i = 0; i < ROW_COUNT; i++) {
        board_[i].resize(COL_COUNT);
    }
}
int check=-1;

        bool WZQ::winner()
        {
        // 横向
        for (int i = 0; i < ROW_COUNT; i++) {
        for (int j = 0; j <= COL_COUNT - 5; j++) {
        if (board_[i][j] != piecesort::NONE &&
        board_[i][j] == board_[i][j + 1] &&
        board_[i][j] == board_[i][j + 2] &&
        board_[i][j] == board_[i][j + 3] &&
        board_[i][j] == board_[i][j + 4]) {
        return true;
        }
        }
        }
        // 纵向
        for (int i = 0; i <= ROW_COUNT - 5; i++) {
            for (int j = 0; j < COL_COUNT; j++) {
                if (board_[i][j] != piecesort::NONE &&
                    board_[i][j] == board_[i + 1][j] &&
                    board_[i][j] == board_[i + 2][j] &&
                    board_[i][j] == board_[i + 3][j] &&
                    board_[i][j] == board_[i + 4][j]) {
                    return true;
                }
            }
        }
        // 正斜线
        for (int i = 0; i <= ROW_COUNT - 5; i++) {
            for (int j = 0; j <= COL_COUNT - 5; j++) {
                if (board_[i][j] != piecesort::NONE &&
                    board_[i][j] == board_[i + 1][j + 1] &&
                    board_[i][j] == board_[i + 2][j + 2] &&
                    board_[i][j] == board_[i + 3][j + 3] &&
                    board_[i][j] == board_[i + 4][j + 4]) {
                    return true;
                }
            }
        }
        // 反斜线
        for (int i = 0; i <= ROW_COUNT - 5; i++) {
            for (int j = 4; j < COL_COUNT; j++) {
                if (board_[i][j] != piecesort::NONE &&
                    board_[i][j] == board_[i + 1][j - 1] &&
                    board_[i][j] == board_[i +2][j - 2] &&
                        board_[i][j] == board_[i + 3][j - 3] &&
                        board_[i][j] == board_[i + 4][j - 4]) {
                        return true;
                 }
             }
        }
       return false; // 还没结束
        }
        void WZQ::mousePressEvent(QMouseEvent *event)
        {
              check*=-1;
            if(check==1){
            qDebug() << "mouse pressed";
            int x1 = event->x();
            int y1 = event->y();
            int row1 = y1 / 36;
            int col1 = x1 / 36;
            // 有棋的话不能下
            if (board_[row1][col1] != piecesort::NONE) {
                return;
            }
            piecePress(piecesort::peiyang,col1*36+18,row1*36+18);

            // 你赢
            bool won = winner();
            if (won) {
                QMessageBox::information(this, "战胜黑子", "提米：不敢了~");

                return;
            }
            }
            if(-check==1){
            // 提米移动
            int x2 = event->x();
            int y2 = event->y();

            int row2 = y2 / 36;
            int col2 = x2 / 36;

            // 已有棋子
            if (board_[row2][col2] != piecesort::NONE) {
                return;
            }


            // 下白子
            piecePress(piecesort::nankai,col2*36+18,row2*36+18);

            // 提米赢
          bool  won = winner();
            if (won) {
                QMessageBox::information(this, "黑子获胜", "提米：拥有鸽鸽的力量，胜利是必然的。");
                return;
            }
            }

                }

                void WZQ::paintEvent(QPaintEvent *event)
                {
                QWidget::paintEvent(event);
                QPainter painter(this);
                QPixmap board(":/board.png");
                painter.drawPixmap(0, 0, width(), height(), board); // 棋盘
                }

                bool WZQ::piecePress(piecesort color, int x, int y)
                {
                QLabel* piece = new QLabel(this);
                piece->setFixedSize(36, 36);
                piece->move(x - 18, y - 18);
                piece->setPixmap(QPixmap(color == piecesort::peiyang ? ":/black.png" : ":/white.png").scaled(36,36)); // 根据颜色设置棋子图片
                piece->show(); // 棋子

                // 更新棋盘状态
                int row = (y - 18) / 36;
                int col = (x - 18) / 36;
                board_[row][col] = color;



                return true; // 落子
                }
